package com.orm.analysis.task;

import com.orm.analysis.dao.impl.AddressDAO;
import com.orm.analysis.dao.impl.PerfAnalysisDAO;
import com.orm.analysis.model.Address;
import com.orm.analysis.model.AddressType;
import com.orm.analysis.model.InternationalAddress;
import com.orm.analysis.model.PerfAnalysis;
import com.orm.analysis.util.GlobalConst;
import org.apache.log4j.Logger;
import org.springframework.core.task.TaskExecutor;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: saman
 * Date: 11/3/13
 * Time: 10:17 AM
 * To change this template use File | Settings | File Templates.
 */
public class BulkTaskExecutor {

    Logger log = Logger.getLogger(BulkTaskExecutor.class);

    private TaskExecutor taskExecutor;

    private String addressDataFielDir;


    private AddressDAO addressDAO;


    private PerfAnalysisDAO perfAnalysisDAO;

    public AddressDAO getAddressDAO() {
        return addressDAO;
    }

    public void setAddressDAO(AddressDAO addressDAO) {
        this.addressDAO = addressDAO;
    }

    public PerfAnalysisDAO getPerfAnalysisDAO() {
        return perfAnalysisDAO;
    }

    public void setPerfAnalysisDAO(PerfAnalysisDAO perfAnalysisDAO) {
        this.perfAnalysisDAO = perfAnalysisDAO;
    }

    public BulkTaskExecutor() {
    }

    private class BulkProcessors implements Runnable {

           public BulkProcessors() {
           }

        public void run() {
            try{

          //            List<Address>  addressList1 =  dao.list(10,10);
          //            log.info(">>>" + addressList1.size() );
          //            for(Address address:addressList1){
          //                log.info(">>>" + address.getClass());
          //
          //            }



//                  log.info("File  >>" + addressDataFielDir);
                  BufferedReader br = null;

                  br = new BufferedReader(new FileReader(addressDataFielDir));

                  StringBuilder sb = new StringBuilder();
                  String line = br.readLine();
                  line = br.readLine();//Skip 1 line

                  List<Address> addressList = new ArrayList<Address>();

                  while (line != null) {
//                      log.info("File  >>" + line);
                      //ADDRESS_ID ,STREET ,ZIPCODE ,ADDRESS_TYPE ,LAST_UPDATE

                      Address address = new  Address();

                      String[] data = line.split("[,]");



                      address.setStreet(data[1]);
                      address.setZipcode(data[2]);
                      //HOME, OFFICE, SHIPPING,BILLING;
                      Character character = data[3].charAt(0);

                      switch (character) {
                          case 'H':
                              address.setAddressType(AddressType.HOME);
                              break;
                          case 'O':
                              address.setAddressType(AddressType.OFFICE);
                              break;
                          case 'S':
                              address.setAddressType(AddressType.SHIPPING);
                              break;
                          case 'B':
                              address.setAddressType(AddressType.BILLING);
                              break;
                          default:
                              address.setAddressType(AddressType.HOME);
                              break;
                      }


                      address.setLastUpdate(GlobalConst.simpleDateFormat.parse(data[4].trim()));


                      if (data[0].equals("1")) {
                          InternationalAddress interAddressService = new InternationalAddress(address);
                          interAddressService.setCountry(data[5].trim());
                          interAddressService.setContinent(data[6].trim());
                          addressList.add(interAddressService);
                      } else {

                          addressList.add(address);
                      }


                      line = br.readLine();



                  }

                log.info("File END >>" );
                     if(addressList.size()>0){
                         long startTime = System.currentTimeMillis();
                         addressDAO.saveList(addressList);
                         long endTime = System.currentTimeMillis();
                         perfAnalysisDAO.save(new PerfAnalysis(GlobalConst.criteriaA2,"1","Addresses" , "executeImproter" , GlobalConst.ormTool_H, endTime- startTime,addressList.size()));
                     }


                  br.close();

                  }catch(Exception e ){
                log.info("Bulk error>>" );
                      e.printStackTrace();
                  }


             }
         }

       public BulkTaskExecutor(TaskExecutor taskExecutor) {
           this.taskExecutor = taskExecutor;
       }


    public void executeBulkTaskProcessors(String addressDataFielDir ) {

           this.addressDataFielDir= addressDataFielDir;
           taskExecutor.execute(new BulkProcessors());
       }
}
